package com.suypower.carManage.action;

import com.jarp.db.DAO;
import com.opensymphony.xwork2.ActionSupport;
import com.suypower.carManage.comm.ConstUtil;

import javax.servlet.ServletException;
import java.util.ArrayList;

/**
 * Created by zhaoyuan on 14-2-18.
 */
public class RoleAction extends ActionSupport {

    private String ID;
    private String NAME;
    private String roleid;
    private String userid;
    private String type;
    private String access;

    public String execute() {
        String res = "";
        DAO dao = new DAO();
        String sql = "";
        if (ID != null && !ID.equals("")) {
            sql = "update tb_role set name='" + NAME + "' where id=" + ID;
        } else {
            sql = "insert into tb_role(name) values('" + NAME + "')";
        }
        System.out.println(sql);
        try {
            int count = dao.execCommand(ConstUtil.DBNAME, sql);
            if (count > 0) {
                res = SUCCESS;
            } else {
                res = ERROR;
            }
        } catch (ServletException e) {
            e.printStackTrace();
            res = ERROR;
        }
        return res;
    }


    /**
     * 角色授权
     *
     * @return
     */
    public String setRoleUser() {
        DAO dao = new DAO();
        String res = "";
        ArrayList<String> list = new ArrayList<String>();
        String[] accessids = access.split(",");
        for (String ass : accessids) {
            list.add("insert into tb_role_access (roleid,accessid,leavel) values (" + roleid + "," + ass + "," + type + ")");
        }
        String sql = "delete from tb_role_access where roleid=" + roleid;
        System.out.println(list);
        try {
            dao.execCommand(ConstUtil.DBNAME, sql);
            boolean b = dao.execCommand(ConstUtil.DBNAME, list);
            if (b) {
                res = SUCCESS;
            } else {
                res = ERROR;
            }
        } catch (ServletException e) {
            e.printStackTrace();
            res = ERROR;
        }
        return res;
    }

    /**
     * 添加该角色下的用户
     *
     * @return
     */
    public String addUsers() {
        String res = "";
        String[] users = userid.split(",");
        ArrayList<String> delete_list = new ArrayList<String>();
        ArrayList<String> add_list = new ArrayList<String>();
        for (int i = 0; i < users.length; i++) {
            delete_list.add(String.format("DELETE FROM tb_user_role WHERE userid = %s", users[i]));
            add_list.add(String.format("INSERT INTO tb_user_role (userid,roleid) VALUES (%s,%s)", users[i], roleid));
        }

        DAO dao = new DAO();
        try {
            boolean deleteState = dao.execCommand(ConstUtil.DBNAME, delete_list);
            if (deleteState) {
                boolean addState = dao.execCommand(ConstUtil.DBNAME, add_list);
                if (addState) {
                    res = SUCCESS;
                } else {
                    res = ERROR;
                }
            } else {
                res = ERROR;
            }
        } catch (ServletException e) {
            e.printStackTrace();
            res = ERROR;
        }
        return res;
    }

    /**
     * 删除用户
     * @return
     */
    public String deleteUser() {
        String res = "";
        String[] users = userid.split(",");
        ArrayList<String> delete_list = new ArrayList<String>();
        for (int i = 0; i < users.length; i++) {
            delete_list.add(String.format("DELETE FROM tb_user_role WHERE userid = %s", users[i]));
        }

        DAO dao = new DAO();
        try {
            boolean deleteState = dao.execCommand(ConstUtil.DBNAME, delete_list);
            if (deleteState) {
              res=SUCCESS;
            } else {
                res = ERROR;
            }
        } catch (ServletException e) {
            e.printStackTrace();
            res = ERROR;
        }
        return res;
    }


    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getNAME() {
        return NAME;
    }

    public void setNAME(String NAME) {
        this.NAME = NAME;
    }

    public String getRoleid() {
        return roleid;
    }

    public void setRoleid(String roleid) {
        this.roleid = roleid;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getAccess() {
        return access;
    }

    public void setAccess(String access) {
        this.access = access;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }
}
